<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>登陆</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
</head>
<style>
    .loginbody {
        width: 100%;
        height: 100%;
        min-width: 1000px;
        background-image: url("http://oss.fanchencloud.cn//blog/202205140211064.png");
        background-size: 100% 100%;
        background-position: center center;
        overflow: auto;
        background-repeat: no-repeat;
        position: fixed;
        line-height: 100%;
        padding-top: 150px;
    }

    /*.loginbody:after {*/
    /*    content: "";*/
    /*    width: 100%;*/
    /*    height: 100%;*/
    /*    position: absolute;*/
    /*    left: 0;*/
    /*    top: 0;*/
    /*    !* 从父元素继承 background 属性的设置 *!*/
    /*    background: inherit;*/
    /*    !*背景模糊*!*/
    /*    filter: blur(4px);*/
    /*}*/

    .logintext {
        margin-bottom: 20px;
        line-height: 50px;
        text-align: center;
        font-size: 30px;
        font-weight: bolder;
        color: white;
        text-shadow: 2px 2px 4px #000000;
    }

    .logindata {
        width: 400px;
        height: 300px;
        transform: translate(-50%);
        margin-left: 50%;
    }

    .tool {
        display: flex;
        justify-content: space-between;
        color: #606266;
    }

    .butt {
        margin-top: 10px;
        text-align: center;
    }

    .shou {
        cursor: pointer;
        color: #606266;
    }

    /*ui*/
    /* /deep/ .el-form-item__label {
      font-weight: bolder;
      font-size: 15px;
      text-align: left;
    }

    /deep/ .el-button {
      width: 100%;
      margin-bottom: 10px;

    } */
</style>
<body>
<div id="loginApp">
    <div class="loginbody">
        <div class="logindata">
            <div class="logintext">
                <h2>课堂教学管理系统设计与实现</h2>
            </div>
            <div class="formdata">
                <el-form ref="form" :model="form" :rules="rules">
                    <el-form-item prop="username">
                        <el-input
                                v-model="form.username"
                                clearable
                                placeholder="请输入账号">
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input
                                v-model="form.password"
                                clearable
                                placeholder="请输入密码"
                                show-password>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="userType">
                        <el-radio v-model="form.userType" label="1">管理员</el-radio>
                        <el-radio v-model="form.userType" label="2">教师</el-radio>
                    </el-form-item>
                </el-form>
            </div>
            <div class="tool">
                <div>
                    <el-checkbox v-model="checked">记住密码
                    </el-checkbox>
                </div>
                <div>
                    <span class="shou" @click="forgetPassword">忘记密码？</span>
                </div>
            </div>
            <div class="butt">
                <el-button type="primary" @click.native.prevent="login('form')">登录
                </el-button>
                <el-button class="shou" @click="forgetPassword">注册</el-button>
            </div>
        </div>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
<!-- axios -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- 引入element-ui组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>

<script>
    const signTaskList = new Vue({
        el: '#loginApp',
        data() {
            return {
                form: {
                    password: "",
                    username: "",
                    userType: '',
                },
                checked: false,
                rules: {
                    username: [
                        {required: true, message: "请输入用户名", trigger: "blur"},
                        {max: 10, message: "不能大于10个字符", trigger: "blur"},
                    ],
                    password: [
                        {required: true, message: "请输入密码", trigger: "blur"},
                        {max: 10, message: "不能大于10个字符", trigger: "blur"},
                    ],
                    userType: [
                        {required: true, message: '请选择用户角色', trigger: 'change'}
                    ],
                },
            };
        },
        mounted() {
            if (localStorage.getItem("news")) {
                this.form = JSON.parse(localStorage.getItem("news"))
                this.checked = true
            }
        },
        methods: {
            login: function (form) {
                const _this = this;
                this.$refs[form].validate((valid) => {
                    if (valid) {
                        console.debug(this.form.username)
                        console.debug(this.form.password)
                        console.debug(this.form.userType)

                        let loginUrl = '/jspmk28g8'
                        // 管理员登录
                        if (this.form.userType == 1) {
                            loginUrl += '/users/login'
                        } else {
                            // 教师登录
                            loginUrl += '/jiaoshi/login'
                        }
                        axios({
                            method: 'post',
                            url: loginUrl,
                            data: {
                                username: this.form.username,
                                password: this.form.password,
                            },
                            transformRequest: [
                                function (data) {
                                    let ret = ''
                                    for (let it in data) {
                                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                                    }
                                    ret = ret.substring(0, ret.lastIndexOf('&'));
                                    return ret
                                }
                            ],
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded'
                            }
                        }).then(function (response) {
                            console.debug(response);
                            const {code, msg, data} = response.data
                            // 登录成功
                            if (code === 200) {
                                window.sessionStorage.setItem('accountTableName', _this.getAccountTableName)
                                window.sessionStorage.setItem('username', _this.form.username);
                                window.sessionStorage.setItem('token', data.token);
                                window.sessionStorage.setItem('role', _this.form.userType == '1' ? "管理员" : "教师");
                                _this.getSessionInfo()
                            } else {
                                _this.$message.error(msg)
                            }
                        }).catch(function (error) {
                            console.debug(error);
                            _this.$message.error('登录失败')
                        });
                    } else {
                        return false;
                    }
                });
            },
            getSessionInfo: function () {
                const _this = this;
                axios({
                    url: '/jspmk28g8/' + this.getAccountTableName + "/session",
                    method: 'get',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                        'token': window.sessionStorage.getItem('token')
                    }
                }).then(function (response) {
                    console.debug(response);
                    const {code, msg, data} = response.data
                    if (code == 0) {
                        window.sessionStorage.setItem('userid', data.id);
                        window.location.href = "${pageContext.request.contextPath}/index.jsp";
                    } else {
                        _this.$message.error('登录失败')
                    }
                }).catch(function (error) {
                    console.debug(error);
                    _this.$message.error('登录失败')
                });

            },
            forgetPassword: function () {
                this.$message({
                    type: "info",
                    message: "功能尚未开发额😥",
                    showClose: true
                })
            },
            register() {
            },
        },
        computed: {
            getAccountTableName() {
                return this.form.userType == 1 ? 'users' : 'jiaoshi'
            }
        }
    });
</script>
</body>
</html>